package com.pri.chat.nim.session.action;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.google.gson.Gson;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.business.session.actions.BaseAction;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.VideoGiftBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.pri.chat.activity.VipCenterActivity;
import com.pri.chat.adapter.GiftListAdapter;
import com.pri.chat.nim.DemoCache;
import com.pri.chat.nim.session.extension.GiftAttachment;
import com.pri.chat.utils.Des3Util;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxDeviceTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 类描述：
 *
 * @author mhd
 */
public class GiftAction extends BaseAction {
    private List<VideoGiftBean> mList = new ArrayList<>();


    /**
     * 构造函数
     */
    public GiftAction() {
//        super(R.drawable.ic_chat_gift, R.string.gift);
        super(R.drawable.nim_message_plus_gift_selector, R.string.gift);

    }

    @Override
    public void onClick() {
        getGiftList();
    }


    private void getGiftList() {
        /**
         * 获取礼物列表
         */mList.clear();

        SubscriberOnNextListener<BaseBean<List<VideoGiftBean>>> onNextListener = baseBean -> {
            mList.addAll(baseBean.getData());
            initPopup();
        };
        HttpMethods.getInstance().listPresent(new ProgressSubscriber<>(onNextListener, DemoCache.getContext(), false));
    }

    private void initPopup() {
        View view = View.inflate(getActivity(), R.layout.dialog_give_gift, null);
        PopupWindow popupWindow;
        popupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(view);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(null);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        ImageView ivClose = view.findViewById(R.id.iv_close);
        RecyclerView rvgifts = view.findViewById(R.id.rv_gifts);
        int mHeight = (int) (RxDeviceTool.getScreenWidth(getActivity()) / 4 * 1.17);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rvgifts.getLayoutParams();
//        if (mList.size() > 4) {
        params.height = 1000;
//        } else {
//            params.height = mHeight;
//        }
        rvgifts.setLayoutParams(params);
        rvgifts.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        GiftListAdapter mAdapter = new GiftListAdapter(R.layout.item_gift_list, mHeight);
        rvgifts.setAdapter(mAdapter);
        mAdapter.setNewData(mList);
        mAdapter.setOnItemClickListener((adapter, view1, position) -> {
            postGiveGift(position);
            popupWindow.dismiss();
        });
        ivClose.setOnClickListener(v -> popupWindow.dismiss());
    }

    /**
     * 赠送礼物
     */
    private void postGiveGift(int position) {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            if (baseBean.isSuccess()) {
                String id = mList.get(position).getId();
                final String giftImage = mList.get(position).getImagePath();
                String giftName = mList.get(position).getName();
                String money = mList.get(position).getPrice();
                GiftAttachment attachment = new GiftAttachment();
                attachment.setDescribe(giftName);
                attachment.setMoney(Integer.parseInt(money));
                String imageulr;
                imageulr = giftImage;
                attachment.setImageUrl(imageulr);
                IMMessage message = MessageBuilder.createCustomMessage(getAccount(), getSessionType(), attachment);
                sendMessage(message);
            } else {
                initDialog("您的金币不足，请充值金币！");
            }
        };
        Map<String, Object> params = new HashMap<>();//登录上传参数
        params.put("sendMemberId", SharedHelper.readId(getActivity()));
        params.put("receiverMemberId", getAccount());
        params.put("presentId", mList.get(position).getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().daShangPresent(new ProgressSubscriber<>(onNextListener, getActivity(), false), requestBody);
    }

    private void initDialog(String content) {
        RxDialogSureCancel rxDialogEditSureCancel = new RxDialogSureCancel(getActivity());
        rxDialogEditSureCancel.getLogoView().setVisibility(View.GONE);
        rxDialogEditSureCancel.getTitleView().setTextSize(18);
        rxDialogEditSureCancel.getTitleView().setTextColor(Color.BLACK);
        rxDialogEditSureCancel.setTitle("提示");
        rxDialogEditSureCancel.setSure("去充值");
        rxDialogEditSureCancel.getSureView().setTextColor(getActivity().getResources().getColor(R.color.orange));
        rxDialogEditSureCancel.setCancel("取消");
        rxDialogEditSureCancel.setContent(content);
        rxDialogEditSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NimUIKitImpl.getSessionListener().openVipActivity(getActivity(),1);
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.show();
    }
}
